var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var compress = require('compression');
var index = require('./routes/index');

var app = express();
app.use(compress());



// view engine setup
app.set('views', path.join(__dirname, 'public'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
// app.use(require('connect-livereload')());
app.use(logger('dev'));
// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public'), {maxAge: '365d'}));



/**
 * 运行环境参数设置
 */
var apiServer = 'mlab-data.leanapp.cn';


//应用启动检测
//app.use(AV.Cloud);
//添加反向代理访问路径
var proxy = require('express-http-proxy');
app.use('/dataserver', proxy(apiServer, {
    limit: "10mb",
    forwardPath: function (req, res) {
        return require('url').parse(req.url).path;
    }
}));
app.use('/', index);






// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.json({errorMessage: err.message});
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.json({errorMessage: err.message});
});

var PORT = 3000;
app.listen(PORT, function () {
  console.log('Node app is running, port:', PORT);
});

module.exports = app;
